// pages/index/history.js
var app = getApp();
Page({
  data: {
    is_empty: false,
    history: []
  },
  onLoad: function() {
    var _this = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      app.util.newLogin(app, function (res) {
      if(res){
          _this.getHistory();
        }
      });
      return;
    }
    this.getHistory();
  },
  onShow: function() {
    var carts = wx.getStorageSync('carts');
    var cart_count = 0;
    var shop_id = wx.getStorageSync('shop_id');
    if (carts && shop_id > 0) {
      carts.forEach(function (cart) {
        if (cart.shopId == shop_id) {
          cart_count = cart.products.length;
        }
      })
    }
    this.setData({ cart_count: cart_count })
  },
  getHistory: function(page) {
    var that = this;
    wx.showLoading({ title: '数据加载中...', mask: true });
    app.func.req('users/history', {
    }, 'GET', function (res) {
        console.log(res)
      if (res.code == "success") {
        var history = res.result;
        history.map(
            (el, i) => {
                if (el.descrip.length >= 12)
                    el.descrip = el.descrip.substr(0, 12) + "...";
            }
        )
        that.setData({
          history: history,
          is_empty: false
        });
      }
      wx.hideLoading();
    }, {
        'token': wx.getStorageSync('token')
      });
  },
 
})